package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.model.Dto.Ids;
import org.example.model.table.Score;
import org.example.model.vo.ScoreVo;
import org.example.service.ScoreService;
import org.example.util.CommonRequest;
import org.example.util.CommonRequestHandle;
import org.example.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/score")
@CrossOrigin(origins = "*",maxAge = 3600)
public class ScoreController {
    @Resource
    ScoreService scoreService;
    @GetMapping("/getAllScore")
    public Result getAllScore(
            @RequestParam(defaultValue = "1",required = false) int page,
            @RequestParam(defaultValue = "10",required = false) int size
    ){
        return scoreService.getAllScore(page, size);
    }
    @GetMapping("/getAllScoreByUserId")
    public Result getAllScoreByUserId(
            @RequestParam(defaultValue = "1",required = false) int page,
            @RequestParam(defaultValue = "10",required = false) int size,
            @RequestParam Long id
    ){
        return scoreService.getAllScoreByUserId(page, size, id);
    }
    @GetMapping("/getAllScoreByClassId")
    public Result getAllScoreByClassId(
            @RequestParam(defaultValue = "1",required = false) int page,
            @RequestParam(defaultValue = "10",required = false) int size,
            @RequestParam Long id
    ){
        return scoreService.getAllScoreByClassId(page, size, id);
    }
    @GetMapping("/getAllScoreByTeacherId")
    public Result getAllScoreByTeacherId(
            @RequestParam(defaultValue = "1",required = false) int page,
            @RequestParam(defaultValue = "10",required = false) int size,
            @RequestParam Long id
    ){
        return scoreService.getAllScoreByTeacherId(page, size, id);
    }
    @DeleteMapping("/deleteScoreById")
    public Result deleteScoreById(@RequestParam(required = false) Long id){
        if(id == null)return Result.error().data("msg","请给一个成绩id").setCode(403);
        return scoreService.deleteScoreById(id);
    }
    @DeleteMapping("/deleteScoreByIds")
    public Result deleteScoreByIds(@RequestBody CommonRequest commonRequest){
        Ids request = CommonRequestHandle.getRequest(commonRequest, Ids.class);
        return scoreService.deleteScoreByIds(request.getIds());
    }
    @PostMapping("/changeScore")
    public Result changeScore(@RequestBody CommonRequest commonRequest){
        Score request = CommonRequestHandle.getRequest(commonRequest, Score.class);
        return scoreService.changeScore(request);
    }
    @PostMapping("/addScore")
    public Result addScore(@RequestBody CommonRequest commonRequest){
        Score request = CommonRequestHandle.getRequest(commonRequest, Score.class);
        return scoreService.addScore(request);
    }

    @GetMapping("/getScorePage")
    public Result getScorePage(
            @RequestParam(defaultValue = "1",required = false) Integer page,
            @RequestParam(defaultValue ="10",required = false, name = "limit") Integer size,
            ScoreVo scoreVo
    ){
        return scoreService.getScorePage(page, size, scoreVo);
    }
}
